package com.xvyy.tingshu.user.api;

import com.xvyy.tingshu.common.login.annotation.TsLogin;
import com.xvyy.tingshu.common.result.Result;
import com.xvyy.tingshu.common.util.AuthContextHolder;
import com.xvyy.tingshu.user.service.UserInfoService;
import com.xvyy.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "微信授权登录接口")
@RestController
@RequestMapping("/api/user/wxLogin")
@Slf4j
public class WxLoginApiController {

    @Autowired
    private UserInfoService userInfoService;

    //http://192.168.217.1:8500/api/user/wxLogin/wxLogin/0e1lbHkl2EH0Se4Oionl2HYvCS0lbHkF
    @GetMapping("/wxLogin/{code}")
    @Operation(summary = "微信授权登录接口")
    public Result<Map<String, Object>> wxLogin(@PathVariable(value = "code") String code) {
        Map<String, Object> map = userInfoService.wxLogin(code);
        return Result.ok(map);
    }


    @GetMapping("/refreshToken")
    @Operation(summary = "刷新token接口")
    public Result<Map<String, Object>> refreshToken() {
        Map<String, Object> map = userInfoService.refreshToken();
        return Result.ok(map);
    }

    // http://192.168.217.1:8500/api/user/wxLogin/getUserInfo
    @GetMapping("getUserInfo")
    @Operation(summary = "获取用户信息")
    @TsLogin// 自定义注解，用于校验用户是否登录
    public Result<UserInfoVo> getUserInfo() {
        // 1. 从认证中心的ThreadLocal中获取用户id
        Long userId = AuthContextHolder.getUserId();
        UserInfoVo userInfoVo = userInfoService.getUserInfoVo(userId);
        return Result.ok(userInfoVo);
    }

    // http://192.168.217.1:8500/api/user/wxLogin/updateUser
    @PostMapping("/updateUser")
    @Operation(summary = "微信登录更新用户信息")
    @TsLogin
    public Result<UserInfoVo> updateUser(@RequestBody UserInfoVo userInfoVo) {
        userInfoService.updateUser(userInfoVo);
        return Result.ok(userInfoVo);
    }

}
